import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/game',
    children: [
      {
        path: '/index',
        name: 'Index',
        // route level code-splitting
        // this generates a separate chunk (index.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "index" */ '../views/index/Index.vue'),
        meta: {
          title: '首页'
        }
      },
      {
        path: '/game',
        name: 'Game',
        // route level code-splitting
        // this generates a separate chunk (game.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "game" */ '../views/game/Game.vue'),
        meta: {
          title: '游戏'
        },
        redirect: '/myMonkey',
        children: [
          {
            path: '/myMonkey',
            name: 'MyMonkey',
            // route level code-splitting
            // this generates a separate chunk (myMonkey.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import(/* webpackChunkName: "myMonkey" */ '../views/game/myMonkey/MyMonkey.vue'),
            meta: {
              title: '我的猩猩'
            }
          },
          {
            path: '/operationMonkey',
            name: 'OperationMonkey',
            // route level code-splitting
            // this generates a separate chunk (operationMonkey.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import(/* webpackChunkName: "operationMonkey" */ '../views/game/myMonkey/OperationMonkey.vue'),
            meta: {
              title: '我的猩猩'
            }
          },
          {
            path: '/drawMonkey',
            name: 'DrawMonkey',
            // route level code-splitting
            // this generates a separate chunk (drawMonkey.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import(/* webpackChunkName: "drawMonkey" */ '../views/game/drawMonkey/DrawMonkey.vue'),
            meta: {
              title: '猩猩抽卡'
            }
          },
          {
            path: '/monkeyWorking',
            name: 'MonkeyWorking',
            // route level code-splitting
            // this generates a separate chunk (monkeyWorking.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import(/* webpackChunkName: "monkeyWorking" */ '../views/game/monkeyWorking/MonkeyWorking.vue'),
            meta: {
              title: '打工'
            }
          }
        ]
      },
    ]
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next()
})



export default router
